var myApp = angular.module('myApp', []);
myApp.controller('SyslogCtrl', function ($scope, $http, $filter) {
  $scope.init = function (params) {
    $scope.syslogList = {}
    $scope.editItem = {}
    $scope.pageNow = 1
    $scope.addSyslogData = {
      status: true,
      protocol: 'udp',
      server_ip: '',
      server_port: '',
    }
    $scope.getPage($scope.pageNow)
  }
  $scope.getPage = function (pageNow) {
    pageNow = pageNow ? pageNow : 1;
    $http({
      method: "get",
      url: "/syslog/list?page=" + pageNow + "&rows=10",
    }).then(function (res) {
      $scope.syslogList = res.data
      $scope.pageNow = res.pageNow
    });
  }

  $scope.addSyslog = function () {
    $scope.addSyslogData = {
      status: true,
      protocol: 'udp',
      server_ip: '',
      server_port: '',
    }
    var W = 540;
    var H = 300;
    var box = null;
    box = zeroModal.show({
      title: '添加SYSLOG配置',
      content: addSyslog,
      width: W + "px",
      height: H + "px",
      ok: true,
      cancel: true,
      okFn: function () {
        $('#port').trigger('change');
        $scope.$apply();
        var rexIp = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
        if ($scope.addSyslogData.server_ip == '') {
          zeroModal.error({
            content: '请填写服务器IP',
          });
          return false;
        }
        if ($scope.addSyslogData.server_port == '') {
          zeroModal.error({
            content: '请填写端口',
          });
          return false;
        }
        if ($scope.addSyslogData.server_ip != '' && !rexIp.test($scope.addSyslogData.server_ip)) {
          zeroModal.error({
            content: 'IP格式不正确',
          });
          return false;
        }
        if ($scope.addSyslogData.server_port != '') {
          if ($scope.addSyslogData.server_port * 1 < 0 || $scope.addSyslogData.server_port * 1 > 65535) {
            zeroModal.error({
              content: '端口不符合范围：0-65535',
            });
            return false;
          }
        }
        console.log($scope.addSyslogData);

        let rqsData = {
          server_ip: $scope.addSyslogData.server_ip,
          server_port: $scope.addSyslogData.server_port,
          protocol: $scope.addSyslogData.protocol,
          status: $scope.addSyslogData.status ? '1' : '0'
        }
        $http.post("/syslog/add-conf", rqsData).then(function success (rsp) {
          if (rsp.data.status == 'success') {
            zeroModal.success('添加成功!');
            console.log(rsp);
            $scope.getPage($scope.pageNow)
          }
          if (rsp.data.status == 'fail') {
            zeroModal.error({
              content: rsp.data.errorMessage,
            });
          }
        }, function err (rsp) {
        });
      },
      onCleanup: function () {
        hideenBox.appendChild(addSyslog);
      }
    });
  }
  $scope.delSyslog = function (item) {
    console.log(item);
    let itemData = item
    zeroModal.confirm({
      content: '操作提示',
      contentDetail: '是否确认删除吗？',
      okFn: function () {
        var loading = zeroModal.loading(4);
        $http.get('/syslog/del-conf?id=' + itemData.id).then(function success (rsp) {
          console.log(rsp);
          zeroModal.close(loading);
          if (rsp.data.status == 'success') {
            $scope.getPage($scope.pageNow)
            zeroModal.success('删除成功!');
          } else {
            zeroModal.error(rsp.data.errorMessage);
          }
        }, function err (rsp) {
          zeroModal.close(loading);
          console.log(rsp);
        });
      },
      cancelFn: function () {
      }
    })
  }

  $scope.editSyslog = function (item) {
    $scope.editItem = JSON.parse(JSON.stringify(item))
    if (item.status == 0) {
      $scope.editItem.status = false
    } else {
      $scope.editItem.status = true
    }
    var W = 540;
    var H = 300;
    var box = null;
    box = zeroModal.show({
      title: '编辑SYSLOG配置',
      content: editSyslog,
      width: W + "px",
      height: H + "px",
      ok: true,
      cancel: true,
      okFn: function () {
        console.log($scope.editItem);
        $('#port2').trigger('change');

        // $scope.$apply();
        var rexIp = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
        if ($scope.editItem.server_ip == '') {
          zeroModal.error({
            content: '请填写服务器IP',
          });
          return false;
        }
        if ($scope.editItem.server_port == '') {
          zeroModal.error({
            content: '请填写端口',
          });
          return false;
        }
        if ($scope.editItem.server_ip != '' && !rexIp.test($scope.editItem.server_ip)) {
          zeroModal.error({
            content: 'IP格式不正确',
          });
          return false;
        }
        if ($scope.editItem.server_port != '') {
          if ($scope.editItem.server_port * 1 < 0 || $scope.editItem.server_port * 1 > 65535) {
            zeroModal.error({
              content: '端口不符合范围：0-65535',
            });
            return false;
          }
        }
        console.log($scope.editItem);

        let rqsData = {
          id: $scope.editItem.id,
          server_ip: $scope.editItem.server_ip,
          server_port: $scope.editItem.server_port,
          protocol: $scope.editItem.protocol,
          status: $scope.editItem.status ? '1' : '0'
        }
        var loading = zeroModal.loading(4);
        $http.post("/syslog/edit-conf", rqsData).then(function success (rsp) {
          zeroModal.close(loading);
          if (rsp.data.status == 'success') {
            zeroModal.success('修改成功!');
            console.log(rsp);
            $scope.getPage($scope.pageNow)
          }
          if (rsp.data.status == 'fail') {
            zeroModal.error({
              content: rsp.data.errorMessage,
            });
          }
        }, function err (rsp) {
          zeroModal.close(loading);
        });
      },
      onCleanup: function () {
        hideenBox.appendChild(editSyslog);
      }
    });
  }


  $scope.init()
});

